package org.timecontrol.rest;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.timecontrol.db.DataFileJDBC;
import org.timecontrol.domain.User;

@Path("/userActivity")
public class UserActivity {

    @Path("AddUser")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void addUser (@QueryParam("name") String name, @QueryParam("lastName") String lastName) {
    	DataFileJDBC dataFileJDBC = new DataFileJDBC();

    	Statement stmt = dataFileJDBC.openConnection();
    	try {
			stmt.executeUpdate("INSERT INTO usertable (lastName, name) VALUES ('" + lastName + "', '" + name + "')");
			if (stmt != null){
				stmt.close(); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    
    @Path("ListUser")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<User> listUser() {
    	DataFileJDBC dataFileJDBC = new DataFileJDBC();
    	Collection<User> userCollection = new ArrayList<User>();
    	ResultSet rs = null;

    	Statement stmt = dataFileJDBC.openConnection();
    	try {
			rs = stmt.executeQuery("SELECT * FROM usertable");
			while (rs.next()) {
				User user = new User();
				user.setLastName(rs.getString("lastName"));
				user.setName(rs.getString("name"));
				userCollection.add(user);
			}
			if (rs != null){
				rs.close(); 
			}
			if (stmt != null){
				stmt.close(); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return userCollection; 
    }
    @Path("DeleteUser")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteUser(@QueryParam("name") String name, @QueryParam("lastName") String lastName){
    	DataFileJDBC dataFileJDBC = new DataFileJDBC();

    	Statement stmt = dataFileJDBC.openConnection();
    	try {
			stmt.executeUpdate("DELETE FROM usertable WHERE lastName = '"+ lastName +"' and name = '"+ name +"'");
			if (stmt != null){
				stmt.close(); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "lastName";
 
    }
    
}